<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
	}

	public function index()
	{
		$this->load->view('login_view');
	}

	//process login
	public function process_login() {
		
		//if not has post request
		if(!isset($_POST['login']))
			redirect('login');

		//else, process it
		
		//get data from login form.
		$usname = $this->input->post('usname');
		$password = $this->input->post('password');

		//call user_model and excute
		$result = $this->user_model->get_user_by($usname, $password); 
		
		//if has data, login success
		if(count($result) > 0) {
			//save data in session
			$usdata = array(
				'id' => $result[0]->id,
				'usname' => usname,
				'name' => $result[0]->name,
				'logged_in' => true
			);

			$this->session->set_userdata('userInfo', $usdata);
			//redirect to home
			redirect('home');
		}else { //login fail
			$this->session->set_userdata('loginError', 'Tên đăng nhập hoặc mật khẩu không đúng!!!');

			//redirect to login form
			redirect('login');
		}
	} 
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */